package stephencarmody.fonttexture;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class ImageFilter extends FileFilter {
	
	private String extension;
	
	public ImageFilter(String extension) {
		this.extension = extension;
	}
	
	/** Accepts all directories and all gif, jpg, tiff, or png files. */
    public boolean accept(File f) {
        if (f.isDirectory()) return true;
        String extension = getExtension(f);
        if ( extension != null && extension.equals(this.extension) ) return true;
        return false;
    }

    //The description of this filter
    public String getDescription() {
        return extension;
    }
    
    public String getExtension() {
    	return extension;
    }
    
    /** Gets the extension of a file. */
    public static String getExtension(File f) {
        String s = f.getName();
        int i = s.lastIndexOf('.');
        return (i > 0 && i < s.length() - 1) ? s.substring(i+1).toLowerCase() : null;
    }
    
}
